import './index.scss';
import tpl from './index.tpl';

export default () => {
  return {
    name: 'toast',
    //配置
    showToast(opt) {
      const oToastBox = document.getElementsByClassName('toast-box')[0];

      //存在什么都不做
      if (oToastBox) {
        return;
      }

      //不存在时 创建
      let oToast = document.createElement('div');
      oToast.className = 'toast-box';
      oToast.innerHTML = tpl().replace(/{{(.*?)}}/g, (node, key) => {
        return {
          icon: opt.icon === 'warning' ? 'warning' : 'check',
          text: opt.title
        } [key];
      });

      document.body.appendChild(oToast);

      //消失
      setTimeout(() => {
        const oToastBox = document.getElementsByClassName('toast-box')[0];
        oToastBox.remove();
      }, opt.duration || 2000)

    }
  }
}